﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ATA.OLSD.ScoringService.Entities;

namespace CPAScoringPerformanceTester.TestTask
{
    public class ScoreTaskSaveTask : TestTaskBase
    {
        private UserAccessTicket ticket;
        private PersonTask task;
        private int index;
        private int total;
        public static string TaskName = "Score Task Save Test";

        public ScoreTaskSaveTask(UserAccessTicket ticket, PersonTask task)
        {
            this.ticket = ticket;
            this.task = task;
        }

        public override void Setup()
        {
            base.Setup();

            index = ScoreTaskSaveSettings.Instance.StartFromLastRead ? 0 : 1;

            if (!ScoreTaskDetailSettings.Instance.TaskId.HasValue)
            {
                Message = ERROR_TASK_NOT_SPECIFIED;
                IsError = true;
                return;
            }

            if (task == null)
            {
                Message = "No task found";
                IsError = true;
            }
            else if (task.PersonDistributedCount <= 0)
            {
                Message = "No response has been allocated";
                IsError = true;
            }
            else
            {
                total = task.PersonDistributedCount;
            }
        }

        public override bool Execute()
        {
            try
            {
                Client = ServiceProxy.NewProxy();
                var record = Client.GetRecord(ticket, new ScoringRequestContext()
                {
                    Type = TaskType.Scoring,
                    TaskId = ScoreTaskDetailSettings.Instance.TaskId.Value
                }, index);

                if (record != null)
                {
                    Random random = new Random(Guid.NewGuid().GetHashCode());
                    foreach (var item in record.ScorePoints)
                    {
                        if (ScoreTaskSaveSettings.Instance.Zero)
                        {
                            item.ScorePointScore = 0;
                        }
                        else if (ScoreTaskSaveSettings.Instance.Full)
                        {
                            item.ScorePointScore = item.ScorePointTotal;
                        }
                        else
                        {
                            item.ScorePointScore = random.Next() % 2 == 0 ? 0 : item.ScorePointTotal;
                        }
                    }

                    if (!Client.Save(ticket, new ScoringRequestContext()
                    {
                        Type = TaskType.Scoring,
                        TaskId = ScoreTaskDetailSettings.Instance.TaskId.Value
                    }, record)) return false;
                    index = ++index > total ? total : index;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                Message = ex.Message;
                return false;
            }
            finally
            {
                Client.CloseConnection();
            }
        }
    }
}
